package com.jdaw.datastructure.stack;

public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack arrayStack=new ArrayStack(5);
        arrayStack.push(2);
        arrayStack.push(8);
        arrayStack.push(6);
        arrayStack.push(4);
        arrayStack.pop();
        arrayStack.list();
    }
}

class ArrayStack{
    private int maxSize;
    private int[] stack;
    private int top=-1;

    public ArrayStack(int maxSize){
        this.maxSize=maxSize;
        stack=new int[this.maxSize];
    }
    public boolean isFull(){
        return top==maxSize-1;
    }
    public boolean isEmpty(){
        return top==-1;
    }
    public void push(int value){
        if(isFull()){
            System.out.println("栈已满");
            return;
        }
        top++;
        stack[top]=value;
    }
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈空，无法抛出");
        }
        int value=stack[top];
        top--;
        return value;
    }
    public void list(){
        if(isEmpty()){
            throw new RuntimeException("栈空，没有数据");
        }
        for(int i=top;i>=0;i--){
            System.out.println(stack[i]);
        }
    }
}